package com.originskyseed.platform.recruit.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.recruit.i18n.RecruitResponseMsg;
import com.originskyseed.platform.recruit.model.RecruitTemplateRelateAnnex;
import com.originskyseed.platform.recruit.service.IRecruitService;
import com.originskyseed.platform.recruit.service.IRecruitTemplateRelateAnnexService;
import com.originskyseed.platform.recruit.vo.request.RecruitTemplateRelateAnnexRequestVO;

/**
 * 招聘模板关联附件信息表Controller类（OSI_RECRUIT_TEMPLATE_RELATE_ANNEX）
 *
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/recruitTemplateRelateAnnex")
@BaseLocaleResource(localeFileName = "i18n_recruit")
public class RecruitTemplateRelateAnnexController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(RecruitTemplateRelateAnnexController.class);

	@DubboReference
	private IRecruitTemplateRelateAnnexService recruitTemplateRelateAnnexService;

	@DubboReference
	private IRecruitService recruitService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/recruitTemplateRelateAnnexList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<RecruitTemplateRelateAnnex>> recruitTemplateRelateAnnexList(HttpServletRequest request) {
		BaseResponseBody<BasePage<RecruitTemplateRelateAnnex>> result = null;
		try {
			// 请求对象
			RecruitTemplateRelateAnnexRequestVO requestVO = this.getParameterBean(RecruitTemplateRelateAnnexRequestVO.class);

			// 分页查询
			BasePage<RecruitTemplateRelateAnnex> page = new BasePage<RecruitTemplateRelateAnnex>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.recruitTemplateRelateAnnexService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<RecruitTemplateRelateAnnex>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<RecruitTemplateRelateAnnex>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/recruitTemplateRelateAnnexSave.json", method = RequestMethod.POST)
	public BaseResponseBody<RecruitTemplateRelateAnnex> recruitTemplateRelateAnnexSave(HttpServletRequest request) {
		BaseResponseBody<RecruitTemplateRelateAnnex> result = null;
		try {
			// 请求对象
			RecruitTemplateRelateAnnex recruitTemplateRelateAnnex = this.getParameterBean(RecruitTemplateRelateAnnex.class);

			if (StringUtils.isNotBlank(recruitTemplateRelateAnnex.getId())) {
				this.recruitTemplateRelateAnnexService.updateByPrimaryKey(recruitTemplateRelateAnnex);
			} else {
				this.recruitTemplateRelateAnnexService.insertSelective(recruitTemplateRelateAnnex);
			}
			result = new BaseResponseBody<RecruitTemplateRelateAnnex>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_SAVE_SUCCESS.getMsg()), recruitTemplateRelateAnnex);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<RecruitTemplateRelateAnnex>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/recruitTemplateRelateAnnexDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> recruitTemplateRelateAnnexDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String id = this.getParameterString("ids", null);
			if (recruitService.selectRecruitTemplateId(id)) {
				return new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.RECRUIT_TEMPLATE_BIND_ERROR.getMsg()), false);
			}
			this.recruitTemplateRelateAnnexService.deleteByPrimaryKey(id);
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 设置默认
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateDefault.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody updateDefault(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			this.recruitTemplateRelateAnnexService.updateIsDefault(id, 1);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_SAVE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectRecruitTemplateList.json", method = RequestMethod.POST)
	public BaseResponseBody selectRecruitTemplateList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			RecruitTemplateRelateAnnex recruitTemplateRelateAnnex = this.getParameterBean(RecruitTemplateRelateAnnex.class);

			// 查询

			List<RecruitTemplateRelateAnnex> recruitTemplateRelateAnnexes = this.recruitTemplateRelateAnnexService.selectList(recruitTemplateRelateAnnex);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_LIST_SUCCESS.getMsg()), recruitTemplateRelateAnnexes);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), RecruitResponseMsg.DATA_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}
